﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Snowdreamist.Windows.Controls;

namespace Snowdreamist.ComponentModel.Primitives
{
    internal class DataTemplateWrapper
    {
        public DataTemplateWrapper(string scope, string viewState, IDataTemplateFactory factory)
        {
            this.Scope = scope;
            this.ViewState = viewState;
            this.Factory = factory;
        }

        public bool IsScopeMatch(string scope)
        {
            if (string.IsNullOrWhiteSpace(scope) && string.IsNullOrWhiteSpace(this.Scope))
                return true;
            else if (this.Scope != null && scope != null)
                // Ingore case by default
                return string.Compare(scope, this.Scope, true) == 0;
            else
                return false;
        }

        public bool IsViewStateMatch(string viewState)
        {
            if (string.IsNullOrWhiteSpace(viewState) && string.IsNullOrWhiteSpace(this.ViewState))
                return true;
            else if (this.ViewState != null && viewState != null)
                // Ingore case by default
                return string.Compare(viewState, this.ViewState, true) == 0;
            else
                return false;
        }

        public string Scope { get; set; }

        public string ViewState { get; set; }

        public IDataTemplateFactory Factory { get; set; }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            if (object.ReferenceEquals(this, obj))
                return true;

            DataTemplateWrapper other = obj as DataTemplateWrapper;
            if (other == null)
                return false;

            return this.IsScopeMatch(other.Scope) && this.IsViewStateMatch(other.ViewState);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}